<template>
    <div id="login-container">
        <div class="login-box">
            <div class="login-title">Login Page</div>
            <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="0">
                <el-form-item prop="username">
                    <span class="form-label">用户名</span>
                    <el-input v-model="loginForm.username" placeholder="请输入用户名" class="login-input"
                        style="width: 80%;"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <span class="form-label">密&emsp;码</span>
                    <el-input type="password" v-model="loginForm.password" placeholder="请输入密码" class="login-input"
                        style="width: 80%;"></el-input>
                </el-form-item>
                <el-form-item prop="code">
                    <span class="form-label">验证码</span>
                    <el-input v-model="loginForm.code" placeholder="请输入验证码" class="login-input"
                        style="width: 50%; display: inline-block;"></el-input>
                    <canvas id="captchaCanvas" width="80" height="40" @click="generateCaptcha"
                        style="cursor: pointer; display: inline-block;"></canvas>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" class="login-button" @click="handleLogin">
                        Login
                    </el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import { drawCode } from '../tool/codeTool';

export default {
    name: "Login",
    data() {
        return {
            loginForm: {
                username: "",
                password: "",
                code: ""
            },
            captchaCode: "",
            rules: {
                username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
                password: [{ required: true, message: "请输入密码", trigger: "blur" }],
                code: [{ required: true, message: "请输入验证码", trigger: "blur" }]
            }
        };
    },
    mounted() {
        this.generateCaptcha();
    },
    methods: {
        handleLogin() {
            if (this.loginForm.code === this.captchaCode) {
                this.$refs.loginForm.validate((valid) => {
                    if (valid) {
                        // Handle login logic here
                        alert("登录成功!");
                    } else {
                        console.log("验证错误!!");
                        return false;
                    }
                });
            } else {
                this.$message.error("验证码错误!");
            }
        },
        generateCaptcha() {
            drawCode("captchaCanvas");
        }
    }
};
</script>

<style>
#login-container {
    background: url('../assets/loginback.jpg') no-repeat center center;
    background-size: cover;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}

.login-box {
    width: 400px;
    padding: 30px;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 10px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
}

.login-title {
    font-size: 24px;
    text-align: center;
    margin-bottom: 20px;
}

.login-input {
    width: 100%;
}

.form-label {
    display: inline-block;
    width: 80px;
}

.login-button {
    width: 100%;
}
</style>